﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ROrganizer.UI.Controllers;
using ROrganizer.UI.Forms.Core;
using BrightIdeasSoftware;

namespace ROrganizer.UI.Forms
{
    internal partial class UpdateForm : Form
    {
        private readonly UpdateFormController controller;

        public UpdateForm(bool hash = true, bool info = true, bool imdb = true, bool meta = true)
        {
            this.controller = new UpdateFormController(hash, info, imdb, meta, this.ProcessException);
            this.InitializeComponent();

            this.olvMessages.Objects = this.controller.Messages;
            this.controller.Messages.CollectionChanged += this.OnChanged;
            this.controller.Changed += this.OnStateChanged;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.olvMessages.Normalize();

            this.btnCancel.Enabled = this.controller.CanStop();
            this.controller.Start();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            e.Cancel = this.controller.IsWorking;
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            this.DialogResult = this.controller.IsChanged ? DialogResult.OK : DialogResult.Cancel;
        }

        private void OnChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (this.SynchronizeInvocation(this.OnChanged, sender, e))
            {
                if (e.Action == NotifyCollectionChangedAction.Reset)
                    this.olvMessages.ClearObjects();

                if (e.OldItems != null && e.OldItems.Count > 0)
                    this.olvMessages.RemoveObjects(e.OldItems);

                if (e.NewItems != null && e.NewItems.Count > 0)
                {
                    this.olvMessages.AddObjects(e.NewItems);
                    this.olvMessages.EnsureModelVisible(e.NewItems[e.NewItems.Count - 1]);
                }
            }
        }

        private void OnStateChanged(object sender, EventArgs e)
        {
            if (this.SynchronizeInvocation(this.OnStateChanged, sender, e))
            {
                this.btnCancel.Enabled = this.controller.CanStop();
            }
        }

        private void OnCancel(object sender, EventArgs e)
        {
            this.ExecuteAndWatch(this.Cancel);
        }

        private void Cancel()
        {
            this.controller.Stop();
            this.Close();
        }

        private void OnFormatRow(object sender, FormatRowEventArgs e)
        {
            var message = e.Model as UpdateFormController.Message;
            if (String.IsNullOrEmpty(message.Action))
                e.Item.ForeColor = Color.Red;
        }
    }
}
